<template>
    <el-dialog
            :title="!dataForm.materialId ? '新增' : '修改'"
            :close-on-click-modal="false"
            :visible.sync="visible">
        <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px">
                            <el-form-item label="物料ID，主键" prop="materialId">
                        <el-input v-model.number="dataForm.materialId" placeholder="物料ID，主键"></el-input>
                </el-form-item>
                <el-form-item label="物料编码，唯一标识" prop="materialCode">
                        <el-input v-model="dataForm.materialCode" placeholder="物料编码，唯一标识"></el-input>
                </el-form-item>
                <el-form-item label="物料名称" prop="materialName">
                        <el-input v-model="dataForm.materialName" placeholder="物料名称"></el-input>
                </el-form-item>
                <el-form-item label="规格型号" prop="specModel">
                        <el-input v-model="dataForm.specModel" placeholder="规格型号"></el-input>
                </el-form-item>
                <el-form-item label="单位（如个、千克等）" prop="unit">
                        <el-input v-model="dataForm.unit" placeholder="单位（如个、千克等）"></el-input>
                </el-form-item>
                <el-form-item label="供应商ID" prop="supplierId">
                        <el-input v-model.number="dataForm.supplierId" placeholder="供应商ID"></el-input>
                </el-form-item>
                <el-form-item label="安全库存" prop="safetyStock">
                        <el-input v-model.number="dataForm.safetyStock" placeholder="安全库存"></el-input>
                </el-form-item>
                <el-form-item label="经济订货量" prop="economicOrderQuantity">
                        <el-input v-model.number="dataForm.economicOrderQuantity" placeholder="经济订货量"></el-input>
                </el-form-item>
                <el-form-item label="物料分类（原材料、辅料等）" prop="materialCategory">
                        <el-input v-model="dataForm.materialCategory" placeholder="物料分类（原材料、辅料等）"></el-input>
                </el-form-item>
                <el-form-item label="创建时间" prop="createdAt">
                        <el-date-picker
                                v-model="dataForm.createdAt"
                                type="datetime"
                                placeholder="创建时间">
                        </el-date-picker>
                </el-form-item>
                <el-form-item label="更新时间" prop="updatedAt">
                        <el-date-picker
                                v-model="dataForm.updatedAt"
                                type="datetime"
                                placeholder="更新时间">
                        </el-date-picker>
                </el-form-item>
                <el-form-item label="物理特性" prop="physicalProperties">
                        <el-input v-model="dataForm.physicalProperties" placeholder="物理特性"></el-input>
                </el-form-item>
                <el-form-item label="质量标准" prop="qualityStandard">
                        <el-input v-model="dataForm.qualityStandard" placeholder="质量标准"></el-input>
                </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
    </el-dialog>
</template>

<script>
    export default {
      data () {
        return {
          visible: false,
          dataForm: {
            materialId: null,
            materialCode: null,
            materialName: null,
            specModel: null,
            unit: null,
            supplierId: null,
            safetyStock: null,
            economicOrderQuantity: null,
            materialCategory: null,
            createdAt: null,
            updatedAt: null,
            physicalProperties: null,
            qualityStandard: null
          },
          dataRule: {
            materialId: [
                        { required: true, message: '物料ID，主键不能为空', trigger: 'blur' }
            ],
            materialCode: [
                        { required: true, message: '物料编码，唯一标识不能为空', trigger: 'blur' }
            ],
            materialName: [
                        { required: true, message: '物料名称不能为空', trigger: 'blur' }
            ],
            specModel: [
                        { required: true, message: '规格型号不能为空', trigger: 'blur' }
            ],
            unit: [
                        { required: true, message: '单位（如个、千克等）不能为空', trigger: 'blur' }
            ],
            supplierId: [
                        { required: true, message: '供应商ID不能为空', trigger: 'blur' }
            ],
            safetyStock: [
                        { required: true, message: '安全库存不能为空', trigger: 'blur' }
            ],
            economicOrderQuantity: [
                        { required: true, message: '经济订货量不能为空', trigger: 'blur' }
            ],
            materialCategory: [
                        { required: true, message: '物料分类（原材料、辅料等）不能为空', trigger: 'blur' }
            ],
            createdAt: [
                        { required: true, message: '创建时间不能为空', trigger: 'blur' }
            ],
            updatedAt: [
                        { required: true, message: '更新时间不能为空', trigger: 'blur' }
            ],
            physicalProperties: [
                        { required: true, message: '物理特性不能为空', trigger: 'blur' }
            ],
            qualityStandard: [
                        { required: true, message: '质量标准不能为空', trigger: 'blur' }
            ]
          }
        }
      },
      methods: {
        init (id) {
          this.dataForm.materialId = id || 0
          this.visible = true
          this.$nextTick(() => {
            this.$refs['dataForm'].resetFields()
            this.dataForm.materialId = id || 0
            if (this.dataForm.materialId) {
              this.$http({
                url: this.$http.adornUrl(`/app/material/info/${this.dataForm.materialId}`),
                method: 'get',
                params: this.$http.adornParams()
              }).then(({data}) => {
                if (data && data.code === 0) {
                  this.dataForm = data.appmaterial
                }
              })
            }
          })
        },
            // 表单提交
        dataFormSubmit () {
          this.$refs['dataForm'].validate((valid) => {
            if (valid) {
              this.$http({
                url: this.$http.adornUrl(`/app/material/${!this.dataForm.materialId ? 'save' : 'update'}`),
                method: 'post',
                data: this.$http.adornData(this.dataForm)
              }).then(({data}) => {
                if (data && data.code === 0) {
                  this.$message({
                    message: '操作成功',
                    type: 'success',
                    duration: 1500,
                    onClose: () => {
                      this.visible = false
                      this.$emit('refreshDataList')
                    }
                  })
                } else {
                  this.$message.error(data.msg)
                }
              })
            }
          })
        }
      }
    }
</script>